package mwebapi

import (
	// "bp/cache"
	"bp/conf"
	"bp/db"

	// "bgs/grpc/client"
	internalHttpMiddleware "bp/internal/middleware/http"
	"bp/internal/render"
	cardDiscountMWebApiImp "bp/internal/rpcserver/mwebapi/card_discount_imp"
	couponMWebApiImp "bp/internal/rpcserver/mwebapi/coupon_imp"
	offTrialMWebApiImp "bp/internal/rpcserver/mwebapi/off_trial_imp"
	offerMWebApiImp "bp/internal/rpcserver/mwebapi/offer_imp"
	promotionMWebApiImp "bp/internal/rpcserver/mwebapi/promotion_imp"
	"bp/internal/service/com"
	"bp/internal/service/entity"
	"bp/internal/util"
	"context"
	"fmt"
	"github.com/go-chi/chi"
	httpMiddleware "github.com/go-chi/chi/middleware"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"net/http"
)

// MWebApiServer :grpc server proxy of webapi
type MWebApiServer struct {
	slog *zap.SugaredLogger
	// cacher *cache.Adapter
}

// NewMWebApiServer initialization
func NewMWebApiServer(
	s *grpc.Server,
	slog *zap.SugaredLogger,
	pgdao *db.PgDao,
	mwebConfPtr *conf.MWebSection,
	vars *conf.VarsSection,
	entityHub *entity.EntityHub,
	comHub *com.ComHub,
	// proxy *client.GrpcClientProxy,
	// adp *cache.Adapter,
) (mws *MWebApiServer) {
	mws = &MWebApiServer{
		slog: slog.With("module", "MWebApiServer"),
		// cacher:           adp,
		// activityService: activityService,
	}

	// couponGrpc.RegisterCouponOpServiceServer(s, mws)
	// slog.Info("couponGrpc register")
	go mws.RegisterWeb(slog, pgdao, mwebConfPtr, vars, entityHub, comHub)

	return
}

// NewContextLogger create a SugaredLogger with contextvalue
func (s *MWebApiServer) NewContextLogger(ctx context.Context) *zap.SugaredLogger {
	return util.NewContextLogger(ctx, s.slog)
}

func (s *MWebApiServer) RegisterWeb(
	slog *zap.SugaredLogger,
	pgdao *db.PgDao,
	mwebConfPtr *conf.MWebSection,
	vars *conf.VarsSection,
	entityHub *entity.EntityHub,
	comHub *com.ComHub,
) (err error) {

	cardDiscountMWebApi := cardDiscountMWebApiImp.NewCardDiscountMWebApi(slog, pgdao, vars, entityHub, comHub)
	couponMWebApi := couponMWebApiImp.New(slog, pgdao, vars, entityHub, comHub)
	offerMWebApi := offerMWebApiImp.NewOfferMWebApi(slog, pgdao, vars, entityHub, comHub)
	offTrialMWebApi := offTrialMWebApiImp.NewOffTrialMWebApi(slog, pgdao, vars, entityHub, comHub)
	promotionMWebApi := promotionMWebApiImp.NewPromotionMWebApi(slog, pgdao, vars, entityHub, comHub)

	mux := chi.NewMux()
	mux.Use(httpMiddleware.Recoverer)
	mux.Use(httpMiddleware.Logger)
	mux.Use(internalHttpMiddleware.WithCors(slog, vars))
	// mux.Use(internalHttpMiddleware.WithWebToken(slog, vars))
	

	mWebApiR := chi.NewRouter()
	mWebApiR.Use(render.WrapResponse(true))

	mWebApiR.Route("/", func(r chi.Router) {
		cardDiscountMWebApi.RegisterToWeb(r)
		couponMWebApi.RegisterToWeb(r)
		offerMWebApi.RegisterToWeb(r)
		offTrialMWebApi.RegisterToWeb(r)
		promotionMWebApi.RegisterToWeb(r)
	})
	mux.Mount("/", mWebApiR)

	pingR := chi.NewRouter()
	pingR.Get("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("ok"))
	})
	mux.Mount("/ping", pingR)

	mWebUrl := fmt.Sprintf("%s:%s", mwebConfPtr.Host, mwebConfPtr.Port)
	slog.Info("mwebapi webserver started")
	http.ListenAndServe(mWebUrl, mux)
	slog.Info("mwebapi webserver stoped")
	return
}
